package com.tqjc.mes.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.mes.entity.SteelInfo;
import com.tqjc.mes.entity.Test;
import com.tqjc.mes.mapper.TestMapper;
import com.tqjc.mes.service.TestService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
    @Resource
    private TestMapper testMapper;

    @DS("mine")
    @Transactional
    @Override
    public void insertAll(List<SteelInfo> list) {
          testMapper.deleteAll();
          for (SteelInfo item:list){
              Test test=new Test();
              test.setMatNo(item.getMatNo());
              test.setMatActThick(item.getMatActThick());
              test.setMatActLen(item.getMatActLen());
              test.setMatActWidth(item.getMatActWidth());
              test.setMatActWt(item.getMatActWt());
              testMapper.insert(test);
          }
    }

    @Override
    public List<Test> selectByMatNo(String matNo) {
        return testMapper.selectByMatNo(matNo);
    }

    @Override
    public List<SteelInfo> mockData(String steelNo) {
        List<Test> tests = this.selectByMatNo(steelNo);
        List<SteelInfo> list = new ArrayList<>(tests.size());
        for (Test test : tests) {
            SteelInfo build = SteelInfo.builder().matActThick(test.getMatActThick()).matActWidth(test.getMatActWidth())
                    .matActLen(test.getMatActLen()).matNo(test.getMatNo()).matActWt(test.getMatActWt()).build();
            list.add(build);
        }
        return list;
    }
}